// Copyright (C) 2009 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Derm.BindGen
{
	class SpecDocFactory
	{
		#region Documentation Generation

		public virtual void WriteDocumentation(string path, SpecParser sParser)
		{
			Dictionary<string, SortedDictionary<string, List<Function>>> deprecatedFunctions = new Dictionary<string,SortedDictionary<string,List<Function>>>();

			foreach (Function function in sParser.Functs.Values) {
				if (function.IsDeprecated == true) {
					string category = (function.Category != null) ? function.Category : String.Empty;

					if (deprecatedFunctions.ContainsKey(category) == false)
						deprecatedFunctions[category] = new SortedDictionary<string,List<Function>>();

					if (deprecatedFunctions[category].ContainsKey(function.ImplementationName) == false)
						deprecatedFunctions[category]["Gl." + function.ImplementationName] = new List<Function>();
					deprecatedFunctions[category]["Gl." + function.ImplementationName].Add(function);
				}
			}

			using (StreamWriter sw = new StreamWriter(path)) {
				StringBuilder sb = new StringBuilder();

				sb.AppendLine("@page page_OpenGL_Deprecations OpenGL Deprecation Lists");
				sb.AppendLine();
				sb.AppendLine("@section section_Deprecated_Functions Deprecated Functions");
				sb.AppendLine();
				sb.AppendLine("The following routines are considered deprecated for a forward OpenGL context (grouped by category):");
				sb.AppendLine();
				foreach (KeyValuePair<string, SortedDictionary<string, List<Function>>> pair in deprecatedFunctions) {
					sb.AppendLine(String.Format("@subsection section_FunctionDeprecated_{0} Category {0}", pair.Key));
					foreach (KeyValuePair<string, List<Function>> pairFunctions in pair.Value) {
						sb.AppendLine(String.Format("- @ref {0}", pairFunctions.Key));
					}
				}
				sb.AppendLine();

				string[] lines = sb.ToString().Split('\n');

				sw.WriteLine();
				foreach (string line in lines)
					sw.Write("/// " + line);
			}
		}

		#endregion
	}
}